package cup.com.icss.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.dao.PeopleRecordMapper;
import cup.com.icss.model.PeopleRecord;
import cup.com.icss.model.User;
import cup.com.icss.utils.LogUtil;
import cup.com.icss.utils.StringUtil;

@Controller
public class peopleRecordController {
	@Resource
	private PeopleRecordMapper peopleRecordMapper;
	//初始化
	@RequestMapping("/peopleRecordInitialist")
	@ResponseBody
	public ModelAndView peopleRecordInitialist(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("storiedBuilding",user.getStoriedBuilding());
			view.addObject("roleName",user.getRoleName());
			view.setViewName("cup/jsp/peopleRecord/peopleRecordList");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	//列表
	@RequestMapping(value="/peopleRecordList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> peopleRecordList(HttpServletRequest request, Integer rows, Integer page, PeopleRecord record){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			page=page==null?1:page;
			rows=rows==null?10:rows;
			int pageNum = page;
			int pageSize = rows;
			int num = peopleRecordMapper.selectPeopleRecordListSize(record).size();
			int pageSizez;
			if(((pageSize*pageNum)-num)<0){
				pageSizez = pageSize;
			}else{
				pageSizez = num-(10*(pageNum-1));
			}
			List<PeopleRecord> somethingLost = peopleRecordMapper.selectPeopleRecordList(pageNum*pageSize-pageSize,pageSizez,record);
			map.put("total", num);
			map.put("rows", somethingLost);
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	//初始化添加
	@RequestMapping("/peopleRecordAdd")
	@ResponseBody
	public ModelAndView peopleRecordAdd(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			view.setViewName("cup/jsp/peopleRecord/peopleRecordAdd");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	//添加
	@RequestMapping(value="/peopleRecordAddDate",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> peopleRecordAddDate(HttpServletRequest request,PeopleRecord record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			record.setStoriedBuilding(user.getStoriedBuilding());
			record.setId(StringUtil.getUUID());
			int inster = peopleRecordMapper.insertSelective(record);
			if(inster>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	//删除
	@RequestMapping(value="/peopleRecordDelete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> peopleRecordDelete(HttpServletRequest request,String id){
		Map<String, String> map = new HashMap<>();
		try {
			int delete = peopleRecordMapper.deleteByPrimaryKey(id);
			if(delete>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	//编辑初始化
	@RequestMapping(value="/peopleRecordEdit",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView peopleRecordEdit(HttpServletRequest request,String id){
		ModelAndView view = new ModelAndView();
		try {
			PeopleRecord peopleRecord = peopleRecordMapper.selectByPrimaryKey(id);
			view.addObject("id",id);
			view.addObject("cnName",peopleRecord.getCnName());
			view.addObject("content",peopleRecord.getContent());
			view.addObject("idCardNumber",peopleRecord.getIdCardNumber());
			view.addObject("phoneNumber",peopleRecord.getPhoneNumber());
			view.setViewName("cup/jsp/peopleRecord/peopleRecordEdit");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	//编辑
	@RequestMapping(value="/peopleRecordEditDate",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> peopleRecordEditDate(HttpServletRequest request,PeopleRecord record){
		Map<String, String> map = new HashMap<>();
		try {
			int update = peopleRecordMapper.updateByPrimaryKeySelective(record);
			if(update>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	//查看
	@RequestMapping(value="/peopleRecordShow",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView peopleRecordShow(HttpServletRequest request,String id){
		ModelAndView view = new ModelAndView();
		try {
			PeopleRecord peopleRecord = peopleRecordMapper.selectByPrimaryKey(id);
			view.addObject("id",id);
			view.addObject("cnName",peopleRecord.getCnName());
			view.addObject("content",peopleRecord.getContent());
			view.addObject("idCardNumber",peopleRecord.getIdCardNumber());
			view.addObject("phoneNumber",peopleRecord.getPhoneNumber());
			view.setViewName("cup/jsp/peopleRecord/peopleRecordShow");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
}
